package it.gat.client.view;

import it.gat.controller.ContrattoController;
import it.gat.model.Cliente;
import it.gat.model.Contratto;

import java.awt.Container;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPanel;

import javax.swing.JButton;
import net.sourceforge.jdatepicker.impl.JDatePanelImpl;
import net.sourceforge.jdatepicker.impl.JDatePickerImpl;
import net.sourceforge.jdatepicker.impl.UtilDateModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.beans.PropertyVetoException;
import java.util.Date;

import javax.swing.SwingConstants;

public class ContrattoInternalFrame extends JInternalFrame implements IClientiSelection{
	private JTextField txtOrePreviste;
	
	private Container container;
	private JInternalFrame that;
	private Contratto contratto;
	private boolean edit;
	private UtilDateModel modelInizio;
	private JTextField txtCliente;
	private UtilDateModel modelFine;
	private Cliente cliente;
	private IClientiSelection thisCallerClientiSelection=this;

	/**
	 * Create the frame.
	 */
	public ContrattoInternalFrame(String title,final boolean edit,final Container desktop,final ContrattiInternalFrame frame) {
		super(title);
		setBounds(0, 0, 645, 203);
		getContentPane().setLayout(null);
		container = desktop;
		that = this;
		this.edit=edit;
		
		JLabel lblTelefono = new JLabel("Ore previste");
		lblTelefono.setBounds(10, 98, 75, 14);
		getContentPane().add(lblTelefono);
		
		txtOrePreviste = new JTextField();
		txtOrePreviste.setHorizontalAlignment(SwingConstants.RIGHT);
		txtOrePreviste.setColumns(10);
		txtOrePreviste.setBounds(90, 95, 112, 20);
		getContentPane().add(txtOrePreviste);
		
		if (edit)
		{
			
		}
		
		JButton btnSalva = new JButton("Salva");
		btnSalva.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (!edit)
				{
					ContrattoController ac = new ContrattoController();
					Contratto contratto = new Contratto();
					contratto.setCliente(cliente);
					contratto.setDataInizio(modelInizio.getValue());
					contratto.setDataFine(modelFine.getValue());
					contratto.setOrePreviste(Integer.parseInt(txtOrePreviste.getText()));
					ac.insertContratto(contratto,cliente);
					frame.refresh();
					try {
						that.setClosed(true);
					} catch (PropertyVetoException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
				else
				{
					ContrattoController ac = new ContrattoController();
					contratto.setCliente(cliente);
					contratto.setDataInizio(modelInizio.getValue());
					contratto.setDataFine(modelFine.getValue());
					contratto.setOrePreviste(Integer.parseInt(txtOrePreviste.getText()));
					ac.updateContratto(contratto);
					frame.refresh();
					try {
						that.setClosed(true);
					} catch (PropertyVetoException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		});
		btnSalva.setBounds(431, 140, 89, 23);
		getContentPane().add(btnSalva);
		
		JButton btnAnnulla = new JButton("Annulla");
		btnAnnulla.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int n = JOptionPane.showConfirmDialog(
						desktop,
					    "Sei sicuro di uscire?",
					    "Conferma",
					    JOptionPane.YES_NO_OPTION,
					    JOptionPane.QUESTION_MESSAGE);
				if (n==JOptionPane.YES_OPTION)
				{
					try {
						that.setClosed(true);
					} catch (PropertyVetoException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		});
		btnAnnulla.setBounds(530, 140, 89, 23);
		getContentPane().add(btnAnnulla);
		
		JLabel lblNewLabel = new JLabel("Cliente");
		lblNewLabel.setBounds(10, 11, 75, 14);
		getContentPane().add(lblNewLabel);
		
		txtCliente = new JTextField();
		txtCliente.setEditable(false);
		txtCliente.setBounds(90, 8, 200, 20);
		getContentPane().add(txtCliente);
		txtCliente.setColumns(10);
		
		JLabel lblDataInizio = new JLabel("Data Inizio");
		lblDataInizio.setBounds(10, 39, 75, 14);
		getContentPane().add(lblDataInizio);
	    
		modelInizio = new UtilDateModel();
		modelInizio.setValue(new Date());
	    JDatePanelImpl datePanelInizio = new JDatePanelImpl(modelInizio);
	    
	    
	    JPanel panel2 = new JPanel();
	    panel2.setBounds(85, 30, 212, 32);
	    getContentPane().add(panel2);
	    JDatePickerImpl datePickerInizio = new JDatePickerImpl(datePanelInizio);
	    panel2.add(datePickerInizio);
	    
	    JLabel lblDataFine = new JLabel("Data Fine");
		lblDataFine.setBounds(10, 70, 75, 14);
		getContentPane().add(lblDataFine);
	    
		modelFine = new UtilDateModel();
		modelFine.setValue(new Date());
	    JDatePanelImpl datePanelFine = new JDatePanelImpl(modelFine);
	    
	    
	    JPanel panel2Fine = new JPanel();
	    panel2Fine.setBounds(85, 60, 212, 32);
	    getContentPane().add(panel2Fine);
	    JDatePickerImpl datePickerFine = new JDatePickerImpl(datePanelFine);
	    panel2Fine.add(datePickerFine);
	    
	    JButton btnCliente = new JButton("...");
		btnCliente.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ClientiSelectionFrame clientiSelectionFrame = new ClientiSelectionFrame("Clienti",desktop,thisCallerClientiSelection);
				desktop.add(clientiSelectionFrame);
				clientiSelectionFrame.setVisible(true);
				clientiSelectionFrame.moveToFront();
			}
		});
		btnCliente.setBounds(294, 7, 26, 23);
		getContentPane().add(btnCliente);

	}
	
	public void setContratto(Contratto contratto)
	{
		this.contratto=contratto;
		this.setClienteSelection(contratto.getCliente());
		modelInizio.setValue(contratto.getDataInizio());
		modelFine.setValue(contratto.getDataFine());
		txtOrePreviste.setText(Integer.toString(contratto.getOrePreviste()));
	}
	
	@Override
	public void setClienteSelection(Cliente cliente) {
		this.cliente=cliente;
		txtCliente.setText(cliente.getRagioneSociale());
	}
}
